package cn.edu.neu.lab603.cloudeval.monitor

import java.util.concurrent.atomic.AtomicLong

import cn.edu.neu.lab603.cloudeval.CloudEval
import cn.edu.neu.lab603.cloudeval.entity.DataCenter.UpdateEvent
import cn.edu.neu.lab603.cloudeval.entity.model.power.PowerModel
import cn.edu.neu.lab603.des.{SimMetric, SimMonitor}
import com.typesafe.scalalogging.LazyLogging

/** 检测能耗的简单监视器。
  *
  * Created by yfwz100 on 16/9/25.
  */
class PowerMetric(protected val powerModel: PowerModel) extends SimMonitor[CloudEval] with SimMetric[Double] with LazyLogging {

  /** 累计能耗 */
  private val accumulatedPower = new AtomicLong

  override def isMatch(context: CloudEval): Boolean = context.getCurrentTask.isInstanceOf[UpdateEvent]

  override def before(context: CloudEval): Unit = {}

  /** 计算计算中心的能耗总计，并累加到能耗中。
    *
    * @param context 执行上下文
    */
  override def after(context: CloudEval): Unit = {
    val power = context.dataCenters.map(_.pmList.map(host => powerModel.getPower(host)).sum).sum
    accumulatedPower.addAndGet(power)
  }

  /** 获取当前累计能耗。
    *
    * @return 累计能耗值
    */
  def getCurrentAccumulatedPower = accumulatedPower.get

  override def getCurrentValue: Double = getCurrentAccumulatedPower
}
